from rest_framework import serializers
from rest_framework.validators import UniqueTogetherValidator
from users.models import GradeStudent
from users.serializer import GradeInfoSerializer,GradeDetailInfoSerializer
from users.serializer import UserSummarySerializer
from course.serializer import CourseSummarySerializer
from course.models import PaperList,Question,Paper
from .models import UserScore,Sign,SignUser,UserAnswerLog
from utils.dfa import get_dfa


class GradeStudentDetailSerializer(serializers.ModelSerializer):
    grade = GradeInfoSerializer()
    user = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )

    class Meta:
        model = GradeStudent
        fields = ("grade","id","user")


class GradeStudentDetailCourseSerializer(serializers.ModelSerializer):
    grade = GradeDetailInfoSerializer(read_only=True)
    user = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )

    class Meta:
        model = GradeStudent
        fields = ("grade","id","user")


class GradeStudentSerializer(serializers.ModelSerializer):
    user = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )

    class Meta:
        model = GradeStudent
        fields = ("grade","user","id")
        validators = [
            UniqueTogetherValidator(queryset=GradeStudent.objects.all(),fields=
            ("user","grade"),
            message="已经收藏")
        ]


from .models import CourseComments


class CourseCommentsSerializers(serializers.ModelSerializer):
    """
    create
    """
    user = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )

    class Meta:
        model = CourseComments
        fields = ["user","course","comments"]

    def validate_comments(self,comments):
        return get_dfa(comments)


class CourseCommentsListSerializers(serializers.ModelSerializer):
    """
    list
    """
    user = UserSummarySerializer()

    class Meta:
        model = CourseComments
        fields = ["user","course","comments"]


class ExamListSerializers(serializers.ModelSerializer):
    course = CourseSummarySerializer(read_only=True)

    class Meta:
        model = PaperList
        fields = ["id", "course", "name","is_allow"]


class QuestionSerializers(serializers.ModelSerializer):
    class Meta:
        model = Question
        exclude = ('course',)


class PaperDetailSerializers(serializers.ModelSerializer):
    question = QuestionSerializers()

    class Meta:
        model = Paper
        fields = ["question","paper_name"]


class ExamDetailSerializers(serializers.ModelSerializer):
    papers = PaperDetailSerializers(many=True)
    course = CourseSummarySerializer()

    class Meta:
        model = PaperList
        fields = ["id", "name","is_allow","papers","course"]


class AnswerSerializers(serializers.Serializer):
    answer = serializers.CharField(read_only=True)
    question = serializers.IntegerField(read_only=True)


class ExamScoreSerializers(serializers.Serializer):
    paper = serializers.CharField(read_only=True)
    answers = AnswerSerializers(many=True,read_only=True)


class UserScoreSerializers(serializers.ModelSerializer):
    paper = ExamListSerializers(read_only=True)

    class Meta:
        model = UserScore
        fields = "__all__"


class SignSerializers(serializers.ModelSerializer):
    grade = GradeInfoSerializer(read_only=True)

    class Meta:
        model = Sign
        fields = "__all__"


class SignUserSerializers(serializers.ModelSerializer):
    students = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )

    def validate(self, attrs):
        sign_grade = Sign.objects.filter(sign_name=attrs.get("sign")).first().grade
        if not GradeStudent.objects.filter(user=attrs.get("students"),grade=sign_grade):
            raise serializers.ValidationError("此班级没有该学生")
        if SignUser.objects.filter(sign__sign_name=attrs.get("sign"),students=attrs.get("students")):
            raise serializers.ValidationError("用户已签到")
        return attrs

    class Meta:
        model = SignUser
        fields = ["sign","students"]


class UserAnswerLogSerializers(serializers.ModelSerializer):
    paper = PaperDetailSerializers()
    user = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )

    class Meta:
        model = UserAnswerLog
        fields = "__all__"


